﻿using System;
using System.Linq.Expressions;

using MicroDb.Options;

using Microsoft.Extensions.DependencyInjection;

namespace MicroDb.MySql
{
    public class MySqlFactory : MicroDbFactory
    {
        private readonly IServiceProvider serviceProvider;

        public MySqlFactory(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        public override bool Is(string type)
        {
            return "MySql".Equals(type, StringComparison.OrdinalIgnoreCase);
        }

        public override SqlExecute CreateExecutor(MicroDbOptions dbSource)
        {
            return ActivatorUtilities.CreateInstance<MySqlExecute>(serviceProvider, dbSource);
        }

        public override SqlResolver CreateResolver(Expression expression)
        {
            var resolver = new MySqlResolver();

            resolver.LoopResolve(expression);

            return resolver;
        }

        public override TableExecute CreateTableExecute(SqlExecute execute)
        {
            return ActivatorUtilities.CreateInstance<MySqlTableExecute>(serviceProvider, execute);
        }
    }
}
